<?php
include '../class/class.user.php';

	if(isset($_REQUEST['aksi_rekam_user'])){
		$nip=$_POST['nip'];
		$password=$_POST['password'];
		$id_kewenangan=$_POST['id_kewenangan'];
		$msg=false;
		$error='';

		session_start();
		$id_parent_user=$_SESSION['id_user'];
		$level=$_SESSION['id_kewenangan'];

		$user= new User();

		$user->nip=$nip;
		$user->password=$password;
		$user->id_parent_user=$id_parent_user;
		$user->user_creating=$id_parent_user;
		$user->id_kewenangan=$id_kewenangan;

		if($level==2){
			$user->status=0;
		}else{
			$user->status=1;
		}

		$userDao= new userDao();
		$cek=$userDao->cekUser($nip);
		if($cek){
			$error="User sudah ada";
		}else {
			$data=$userDao->createUser($user);
			if(!$data){
				$error='Gagal Rekam User';
			}else{
				$msg=true;
			}
		}

		$result=array(
			'msg' => $msg,
			'error' => $error,
			);

		echo json_encode($result);

	}

	if(isset($_REQUEST['aksi_hapus_user'])){
		$id_user=$_REQUEST['id_user'];

		$userDao= new UserDao();

		$user_del=$userDao->deleteUser($id_user);
		$msg=true;
		$error='';

		if($user_del){
			$msg=true;
		}else{
			$error="Gagal Hapus User";
		}

		$data=array(
			'msg' => $msg,
			'error' => $error,
			);

		echo json_encode($data);


	}

	if(isset($_REQUEST['aksi_aktivasi_user'])){
		$id_user=$_REQUEST['id_user'];

		$userDao= new UserDao();

		$user_act=$userDao->aktivasiUser($id_user);
		$msg=false;
		$error='';

		if($user_act){
			$msg=true;
		}else{
			$error="Gagal Aktivasi User";
		}

		$data=array(
			'msg' => $msg,
			'error' => $error,
			);

		echo json_encode($data);


	}

	if(isset($_REQUEST['aksi_get_user_ubah'])){
		$id_user=$_REQUEST['id_user'];

		$userdao=new UserDao();
		$user=new User();
		$user->id_user=$id_user;

		$user_ubah=$userdao->getUserById($user);

		$data=array(
			'id_user' => $user_ubah['id_user'],
			'nip' => $user_ubah['nip'],
			'id_kewenangan' => $user_ubah['id_kewenangan'],
			);

		echo json_encode($data);
	}

	if(isset($_REQUEST['aksi_simpan_ubah_user'])){
		$id_user=$_POST['id_user'];
		$nip=$_POST['nip'];
		$password=$_POST['password'];
		$id_kewenangan=$_POST['id_kewenangan'];
		$msg=false;
		$error='Gagal Ubah User';

		session_start();
		$id_pengubah_user=$_SESSION['id_user'];

		$user= new User();
		$user->id_user=$id_user;
		$user->nip=$nip;
		$user->password=$password;
		$user->id_parent=$id_pengubah_user;
		$user->id_kewenangan=$id_kewenangan;
		$userDao= new userDao();

		$ubah=$userDao->updateUser($user);

		if($ubah){
			$msg=true;
		}

		$data=array(
			'msg' => $msg,
			'error' => $error,
			);

		echo json_encode($data);
	}

	if(isset($_REQUEST['aksi_get_user_ubah_pass'])){
		session_start();

		$id_user=$_SESSION['id_user'];

		$user=new User();
		$user->id_user=$id_user;

		$userDao=new UserDao();

		$user_data=$userDao->getUserById($user);

		$data=array(
			'nip' => $user_data['nip'],
			'id_user' => $id_user,
			);

		echo json_encode($data);
	}

	if(isset($_REQUEST['aksi_ubah_password'])){

		session_start();

		$id_user_pengubah=$_SESSION['id_user'];

		$id_user=$_POST['id_user'];
		$password_lama=md5($_POST['password_lama']);
		$password=$_POST['password'];
		$msg=false;
		$error='Gagal Ubah Password';

		$user=new User();
		$user->id_user=$id_user;

		$userDao= new UserDao();
		$user_data=$userDao->getUserById($user);

		if($password_lama==$user_data['password']){
			$user_ubah=new User();
			$user_ubah->id_user=$id_user;
			$user_ubah->password=$password;
			$user_ubah->nip=$user_data['nip'];
			$user_ubah->id_kewenangan=$user_data['id_kewenangan'];
			$user_ubah->id_parent=$id_user_pengubah;

			$hasil=$userDao->updateUser($user_ubah);
			if($hasil){
				$msg=true;
			}
		}else{
			$error='Password Lama Salah';
		}

		$data=array(
			'msg' => $msg,
			'error' => $error,
			);

		echo json_encode($data);

	}
?>